import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '../store'
Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  routes: routes
})

const permissionRoutes = ['/confirm', '/mine', '/cart']
router.beforeEach((to, from, next) => {
  if (permissionRoutes.includes(to.path)) {
    // 有 token 数据，则说明用户已登录成功，
    // 如果没有 token 数据，则跳转到登录页面
    if (store.state.user.token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router